<?php 
	namespace app\admin\controller;
	use think\Controller;
	use think\Db;
	use think\Session;
	//后台主页
	class user extends Allow
	{
		public function getIndex()
		{
			
			
			$request=request();
              //用户名关键字查询
			$k=$request->param('keywords');
			//电话号码关键字查询
			$p=$request->param('phone');
			//状态关键字查询
			$s=$request->param('static');
			
		    $list=Db::table("user")->where('username',"like","%".$k."%")->where('phone',"like","%".$p."%")
			->where('static',"like","%".$s."%")->paginate(5);
			$data=Db::table('users')->select();

			return $this->fetch('user/index',['list'=>$list,'data'=>$data,'request'=>$request->param()]);
		}
		public function getEdit()
		{
			$request=request();
			//判断是否有ID
			if(!empty($request->param('id')))
			{
				$id=$request->param('id');
				//获取单条数据
				$list=Db::table('user')->where('id',$id)->find();
				//获取uers数据
				$data=Db::table('users')->select();
				//加载模板
			    return $this->fetch('user/edit',['list'=>$list,'data'=>$data]);

			}else
			{
				$this->error('没有相应的数据','/user/index');
				exit;
			}
			
		}
		public function postUpdate()
		{
			$request=request();
			//填写空值则返回
			foreach($_POST as $k=>$v)
			{
				if($v=="")
				{
                  $this->error('请填写完整','/user/index');
				  exit;
				}

			}
			if($_POST['password']!=$_POST['repassword'])
			{
				$this->error('密码不一致','/user/index');
				exit;
			}
			 $result=$this->validate($request->param(),'User');
               if(true!==$result)
              {
                $this->error($result,'/user/index');
                 
			  }
				$id=$request->param('id');
				//获取数据
				$result=$request->except(['repassword','action']);
                //查找数据库数据
                $results=Db::table('user')->where('id',$id)->find();
                if($result['username']==$results['username']&&$result['phone']==$results['phone'])
                {
                	//没有更改username,没有更改手机号码
                	$list=Db::table('user')->where('id',$id)->update($result);
                	if($list)
                		{
                			$this->success('修改成功','/user/index');
                		}else
                		{
                			$this->error('修改失败','/user/index');
                		}
                	
                }elseif($result['username']!=$results['username']&&$result['phone']==$results['phone'])
                {
                	//更改了username,没有更改手机
                	$username=$result['username'];
                	//查询数据库中是否存在这个username
                	$value=Db::table('user')->where('username',$username)->find();
                	if($value)
                	{
                       //存在
                       $this->error('用户名已存在,请再挑选一个','/user/index');
                       exit;
                	}else 
                	{
                		//不存在可以添加
                		$list=Db::table('user')->where('id',$id)->update($result);
                		if($list)
                		{
                			$this->success('修改成功','/user/index');
                		}else
                		{
                			$this->error('修改失败','/user/index');
                		}
                	}
                	
                }elseif($result['username']==$results['username']&&$result['phone']!=$results['phone'])
                {
                    //没有修改用户名,修改了手机
                    $phone=$result['phone'];
                    //查询数据库中是否存在这个phone
                    $value=Db::table('user')->where('phone',$phone)->find();
                    if($value)
                	{
                       //存在
                       $this->error('手机号码已存在','/user/index');
                       exit;
                	}else 
                	{
                		//不存在可以添加
                		$list=Db::table('user')->where('id',$id)->update($result);
                		if($list)
                		{
                			$this->success('修改成功','/user/index');
                		}else
                		{
                			$this->error('修改失败','/user/index');
                		}
                	}
                    
                }elseif($result['username']!=$results['username']&&$result['phone']!=$results['phone'])
                {
                	//修改了手机号码,修改了用户名
                    $username=$result['username'];
                    $value=Db::table('user')->where('username',$username)->find();
                    if($value)
                    {
                    	 $this->error('用户名已存在,请再挑选一个','/user/index');
                       exit;
                    }
                    $phone=$result['phone'];
                     $values=Db::table('user')->where('phone',$phone)->find();
                      if($values)
                    {
                    	 $this->error('手机号码已存在','/user/index');
                       exit;
                    }
                    $list=Db::table('user')->where('id',$id)->update($result);
                		if($list)
                		{
                			$this->success('修改成功','/user/index');
                		}else
                		{
                			$this->error('修改失败','/user/index');
                        }	
               }
		}
		public function getdelete()
		{
			$request=request();
			//获取ID
			$id=$request->param('id');
	        $list=Db::table('users')->where('id',$id)->delete();
	        if($list)
	        {
	        	//删除成功
	        	$this->success('删除成功','/users/index');
	        }else
	        {
	        	//删除失败
	        	$this->success('删除失败','/users/index');
	        }

		}
		public function getadd()
		{
			$list=Db::table('users')->select();
			return $this->fetch('user/add',['list'=>$list]);
		}
		public function postinsert()
		{

			$request=request();

			foreach($_POST as $k=>$v)
			{
				if($v=="")
				{
                  $this->error('请填写完整','/user/add');
				  exit;
				}

			}

			if($_POST['password']!=$_POST['repassword'])
			{
				$this->error('密码不一致','/user/add');
				exit;
			}
			 $result=$this->validate($request->param(),'Users');
               if(true!==$result)
              {
                $this->error($result);
                 return $this->fetch('user/add');
			  }
			   
			    $data=$request->except(['repassword','action']);
			     $data['reg']=time();
				$list=Db::table('user')->insert($data);
				if($data)
				{
					$this->success('添加成功','/user/index');
				}else
				{
					$this->error('添加失败','/user/index');
				}
			
		}
		public function postdelete()
		{
			if($_POST==null)
			{
				$this->redirect('/user/index');
				exit;
			}else
			{
				//获取参数个数
				$b=count($_POST);
               //设置个变量
				$a=0;
				foreach($_POST as $k=>$v)
				{
					$data=Db::table('user')->where('id',$v)->delete();
					if($data)
					{
						//如果删除成功则+1
                     $a+=1;
					}
				}
			}
			//如果删除个数和参数个数一样代表删除成功
			if($b==$a)
			{
				$this->success('删除成功','/user/index');
			}else
			{
				$this->error('删除失败','/user/index');
			}


		}
		public function getlogin()
		{
			//同退出登录的操作一样
			$list=Db::table('user')->where('id',session::get('ids'))->find();
			$list['login']=session::get('times');
			$list=Db::table('user')->where('id',session::get('ids'))->update($list);
			session::delete('ids');
			session::delete('times');
			Session::delete('aname');
			//获取参数
			$request=request();
			$id=$request->param('id');
			//通过id查找数据库
			$data=Db::table('user')->where('id',$id)->find();
			if($data)
			{
			//赋予新的值
			 session::set('ids',$id);
			 session::set('aname',$data['username']);
			 Session::set('times',time());
			 $this->success('请稍等','/admin/index');
			}


			
			

			
		}
	}
 ?>